//
//  AppDelegate.m
//  CwinCar
//
//  Created by dlios on 15/10/15.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "AppDelegate.h"
#import "DataBase.h"
#import <BaiduMapAPI/BMapKit.h>
@interface AppDelegate ()<UIScrollViewDelegate>
@property (nonatomic , strong) BMKMapManager * mapManager ;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.window makeKeyAndVisible];
    [[DataBase shareDataBase]openDB];
    [self.window setBackgroundColor:[UIColor whiteColor]];
    [self.window setRootViewController:[[BaseTabBarViewController alloc]init]];
  
    _mapManager = [[BMKMapManager alloc]init];
    
    BOOL ret = [_mapManager start:@"VG3OzMjZXCYzhYHvBfO2OzcQ" generalDelegate:nil];
    if (!ret) {
        
        NSLog(@"启动失败");
        
    }
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(webChange) name:kReachabilityChangedNotification object:nil];
    self.conet = [Reachability reachabilityForInternetConnection];
    //    开始监听网络
    [self.conet startNotifier];
    [self webChange];
    [self creatNightMoon];
    
#pragma mark 引导页判断
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        
    }else
    {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
    }
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"])
    {
        [self creatGuide];
        
        
    }
    

    return YES;
}
- (void)webChange
{
    NSString *str = [[NSString alloc]init];
    //    检查wifi
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    //    检查是否能连接到2G/3G/4G,wifi
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    if ([wifi currentReachabilityStatus] != NotReachable) {
        //      有wifi
        [[NSUserDefaults standardUserDefaults]setObject:@"yes" forKey:@"status"];
        str = @"boss,当前使用的是WIFI,请随意~";
    }else if ([conn currentReachabilityStatus] != NotReachable)
    {
        [[NSUserDefaults standardUserDefaults]setObject:@"yes" forKey:@"status"];
        str = @"当前使用的是3G/4G网络,注意流量哦~土豪请随意";
    }else
    {
        [[NSUserDefaults standardUserDefaults]setObject:@"no" forKey:@"status"];
        str = @"世界上最遥远的距离就是 ~~~ 没有网 ~~~~ ";
    } 
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:str delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerFireMethod : ) userInfo:alert repeats:YES];
    [alert show];
    
    
  
}

//提示框消失

-(void)timerFireMethod : (NSTimer * )theTimer{
    
    UIAlertView * alert = (UIAlertView * )[theTimer userInfo];
    
    [alert dismissWithClickedButtonIndex:0 animated:NO];
    
   alert = NULL ;
    
}
#pragma mark - 创建夜间模式
- (void)creatNightMoon
{
    //注册通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(nightSwitch:) name:@"night" object:nil];
    self.nightModeView = [[UIView alloc]initWithFrame:self.window.bounds];
    self.nightModeView.userInteractionEnabled = NO;
    self.nightModeView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"nightmoon"]) {
        self.nightModeView.hidden = NO;
        
    }else
    {
        self.nightModeView.hidden = YES;
    }
    [self.window addSubview:self.nightModeView];
}
-(void)nightSwitch:(NSNotification *)center
{
    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"nightmoon"]) {
        
         self.nightModeView.hidden = NO;
        
        
    }else
    {
        self.nightModeView.hidden = YES;
}
}
- (void)creatGuide
{
    self.imgBack = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, mainScreen_Width,mainScree_Height)];
    [self.imgBack setImage:[UIImage imageNamed:@"pic1"]];
    [self.window addSubview:self.imgBack];
    
    self.scView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
    [self.scView setContentSize:CGSizeMake(self.window.frame.size.width * 3, 0)];
    
    self.scView.delegate = self;
    self.scView.showsHorizontalScrollIndicator = YES;
    [self.scView setPagingEnabled:YES];
    [self.window addSubview:self.scView];
    
    UIImageView *onelogo = [[UIImageView alloc]initWithFrame:CGRectMake(mainScreen_Width / 12.5, mainScree_Height / 22.233, mainScreen_Width / 6.25, mainScree_Height / 8.3375)];
    [onelogo setImage:[UIImage imageNamed:[NSString stringWithFormat:@"11111.png"]]];
    [self.scView addSubview:onelogo];
    
    UILabel *nameLab = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width / 3.75, mainScree_Height / 11.116, mainScreen_Width / 3.75, mainScree_Height / 13.34)];
    nameLab.textColor = [UIColor whiteColor];
    nameLab.text = @"Maserati";
    nameLab.font = [UIFont systemFontOfSize:20.];
    [self.scView addSubview:nameLab];
    
    UILabel *oneLab = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width / 7.5, mainScree_Height / 1.361,mainScreen_Width / 1.363, mainScree_Height / 11.116)];
    [self.scView addSubview:oneLab];
    oneLab.text = @"就是这么任性";
    oneLab.textColor = [UIColor whiteColor];
    oneLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:35.f];
    
    UILabel *oneSlogan = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width / 3.409, mainScree_Height / 1.282, mainScreen_Width / 1.5, mainScree_Height / 6.67)];
    oneSlogan.text = @"发现源自内心的力量";
    oneSlogan.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.f];
    oneSlogan.textColor = [UIColor lightGrayColor];
    oneSlogan.numberOfLines = 0;
    [self.scView addSubview:oneSlogan];
    
    UILabel *oneTSlogan = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width / 2.884,mainScree_Height / 1.191, mainScreen_Width / 1.5, mainScree_Height / 6.67)];
    oneTSlogan.text = @"让世界听见你的呐喊";
    oneTSlogan.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.f];
    oneTSlogan.textColor = [UIColor lightGrayColor];
    oneTSlogan.numberOfLines = 0;
    [self.scView addSubview:oneTSlogan];
    //-----------------------------------------------------------------//
    
    UIImageView *twologo = [[UIImageView alloc]initWithFrame:CGRectMake(mainScreen_Width + mainScreen_Width / 12.5, mainScree_Height / 22.233, mainScreen_Width / 6.25, mainScree_Height / 8.3375)];
    [twologo setImage:[UIImage imageNamed:[NSString stringWithFormat:@"331(1).png"]]];
    [self.scView addSubview:twologo];
    
    UILabel *nameTwoLab = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width+ mainScreen_Width / 3.75,mainScree_Height / 11.116, mainScreen_Width / 3.75, mainScree_Height / 13.34)];
    nameTwoLab.textColor = [UIColor whiteColor];
    nameTwoLab.text = @"Porsche";
    nameTwoLab.font = [UIFont systemFontOfSize:20.];
    [self.scView addSubview:nameTwoLab];
    
    UILabel *twoLab = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width + mainScreen_Width / 7.5, mainScree_Height / 1.361, mainScreen_Width / 1.363, mainScree_Height / 11.116)];
    [self.scView addSubview:twoLab];
    twoLab.text = @"就是这么靠谱";
    twoLab.textColor = [UIColor whiteColor];
    twoLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:35.f];
    
    UILabel *twoSlogan = [[UILabel alloc]initWithFrame:CGRectMake( mainScreen_Width +mainScreen_Width / 3.409, mainScree_Height / 1.282, mainScreen_Width / 1.5, mainScree_Height / 6.67)];
    twoSlogan.text = @"创造经典 止于至善";
    twoSlogan.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.f];
    twoSlogan.textColor = [UIColor lightGrayColor];
    twoSlogan.numberOfLines = 0;
    [self.scView addSubview:twoSlogan];
    
    UILabel *twoTSlogan = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width + mainScreen_Width / 2.884, mainScree_Height / 1.191, mainScreen_Width / 1.5, mainScree_Height / 6.67)];
    twoTSlogan.text = @"向往自然 更享于自我";
    twoTSlogan.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.f];
    twoTSlogan.textColor = [UIColor lightGrayColor];
    twoTSlogan.numberOfLines = 0;
    [self.scView addSubview:twoTSlogan];
    
    //-----------------------------------------------------------------//
    
    UIImageView *threelogo = [[UIImageView alloc]initWithFrame:CGRectMake(mainScreen_Width * 2 + mainScreen_Width/ 12.5, mainScree_Height / 22.233, mainScreen_Width/ 6.25,mainScree_Height / 8.3375)];
    [threelogo setImage:[UIImage imageNamed:[NSString stringWithFormat:@"242.png"]]];
    [self.scView addSubview:threelogo];
    
    UILabel *nameThreeLab = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width * 2 + mainScreen_Width / 3.75,mainScree_Height/ 11.116,mainScreen_Width / 1.25, mainScree_Height / 13.34)];
    nameThreeLab.textColor = [UIColor whiteColor];
    nameThreeLab.text = @"Automobili Lamborghini S.p.A.";
    nameThreeLab.font = [UIFont systemFontOfSize:20.];
    [self.scView addSubview:nameThreeLab];
    
    UILabel *threeLab = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width * 2 + mainScreen_Width / 7.5, mainScree_Height / 1.361, mainScreen_Width / 1.363, mainScree_Height / 11.116)];
    [self.scView addSubview:threeLab];
    threeLab.text = @"就是这么霸道";
    threeLab.textColor = [UIColor whiteColor];
    threeLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:35.f];
    [self.scView addSubview:threeLab];
    
    UILabel *threeSlogan = [[UILabel alloc]initWithFrame:CGRectMake( mainScreen_Width * 2 + mainScreen_Width / 3.409, mainScree_Height / 1.282, mainScreen_Width / 1.5, mainScree_Height / 6.67)];
    threeSlogan.text = @"极致、纯粹、不妥协";
    threeSlogan.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.f];
    threeSlogan.textColor = [UIColor lightGrayColor];
    threeSlogan.numberOfLines = 0;
    [self.scView addSubview:threeSlogan];
    
    UIButton *enterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [enterButton setFrame:CGRectMake(self.window.frame.size.width * 2 + mainScreen_Width / 7.5, self.window.frame.size.height - mainScree_Height / 11.116, mainScreen_Width / 1.363, mainScree_Height / 16.675)];
    [enterButton addTarget:self action:@selector(enterClick) forControlEvents:UIControlEventTouchUpInside];
    
    enterButton.layer.borderWidth = 0.5;
    enterButton.layer.cornerRadius = 5;
    enterButton.layer.borderColor = [UIColor cyanColor].CGColor;
    
    [enterButton setTitle:@"E n t e r" forState:UIControlStateNormal];
    [self.scView addSubview:enterButton];

}
- (void)enterClick
{
    [UIView animateWithDuration:0.5 animations:^{
        
        self.backView.hidden = YES;
        self.imgBack.hidden = YES;
        
        [self.scView setFrame:CGRectMake(self.window.frame.size.width, 0, self.window.frame.size.width, self.window.frame.size.height)];
    }];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    NSInteger index = self.scView.contentOffset.x / self.window.frame.size.width;
    CGFloat alpha = 1 - ((self.scView.contentOffset.x - index * self.window.frame.size.width) / self.window.frame.size.width);
    
    self.scView.alpha = alpha;
    
    
    if (self.scView.contentOffset.x == 0) {
        
        [self.imgBack setImage:[UIImage imageNamed:[NSString stringWithFormat:@"pic1"]]];
    }else if (self.scView.contentOffset.x == mainScreen_Width)
    {
        [self.imgBack setImage:[UIImage imageNamed:[NSString stringWithFormat:@"pic2"]]];
        
    }else if (self.scView.contentOffset.x == mainScreen_Width * 2)
    {
        [self.imgBack setImage:[UIImage imageNamed:[NSString stringWithFormat:@"pic3"]]];
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
 [BMKMapView willBackGround];//当应用即将后台时调用，停止一切调用opengl相关的操作。
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
 [BMKMapView didForeGround];//当应用恢复前台状态时调用。
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
